package com.example.xianji.adapter;

import java.util.ArrayList;

import com.example.xianji.Contant;
import com.example.xianji.activity.Main_Liuyan_DetailActivity;
import com.example.xianji.activity.Main_Skill_DetailsActivity;
import com.example.xianji.R;
import com.example.xianji.bean.Collection;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class ZiXun_Skill_Adapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Collection> list;
    //    private SharedPreferences sp;
//    private Editor ed;
    private int come_from;

    public ZiXun_Skill_Adapter(Context mContext, ArrayList<Collection> list, int come_from) {
        super();
        this.mContext = mContext;
        this.list = list;
        this.come_from = come_from;
//        sp = mContext.getSharedPreferences("text", mContext.MODE_PRIVATE);
//        ed = sp.edit();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder mHolder;

        if (convertView == null) {
            mHolder = new Holder();
            convertView = View.inflate(mContext, R.layout.collection_listview_item, null);
            mHolder.collection_title = (TextView) convertView.findViewById(R.id.collection_title);
            mHolder.collection_time = (TextView) convertView.findViewById(R.id.collection_time);
            mHolder.button_delect = (Button) convertView.findViewById(R.id.button_delect);
            mHolder.zixun_content = (TextView) convertView.findViewById(R.id.zixun_content);
            convertView.setTag(mHolder);
        }
        mHolder = (Holder) convertView.getTag();

        if (come_from == Contant.come_from_my_skill_zixun) {
            //我咨询的技术
            mHolder.zixun_content.setVisibility(View.VISIBLE);
            mHolder.zixun_content.setText("咨询内容：  " + list.get(position).getDetail());
        } else {
            mHolder.zixun_content.setVisibility(View.GONE);
        }
        if (come_from == Contant.come_from_my_product_liuyan) {
            //我的产品留言
            mHolder.collection_title.setText(new StringBuffer("我在产品   ").append("<<").
                    append(list.get(position).getTitle()).append(">>  给   ").append(list.get(position).getNickname()).append("  留言"));
        } else if (come_from == Contant.come_from_my_service_liuyan) {
            //我的服务留言
            mHolder.collection_title.setText(new StringBuffer("我在服务   ").append("<<").
                    append(list.get(position).getTitle()).append(">>  给   ").append(list.get(position).getNickname()).append("  留言"));
        } else if (come_from == Contant.come_from_my_qiye_liuyan) {
            //我对店铺的留言
            mHolder.collection_title.setText(new StringBuffer("我在店铺   ").append("<<").
                    append(list.get(position).getTitle()).append(">>  给   ").append(list.get(position).getNickname()).append("  留言"));

        } else if (come_from == Contant.come_from_my_skill_zixun) {
            mHolder.collection_title.setText(list.get(position).getTitle());
        } else if (come_from == Contant.come_from_my_receiver_fuwu_liuyan) {
            //我收到的服务留言（企业账号）
            mHolder.collection_title.setText(new StringBuffer(list.get(position).getNickname()).append("   在").append("<<").
                    append(list.get(position).getTitle()).append(">>  ").append("  给我留言"));
        } else if (come_from == Contant.come_from_my_receiver_qiye_liuyan) {
            //我收到的店铺留言（企业账号）
            mHolder.collection_title.setText(new StringBuffer(list.get(position).getNickname()).append("   在").append("<<").
                    append(list.get(position).getTitle()).append(">>  ").append("  给我留言"));
        } else if (come_from == Contant.come_from_my_receiver_product_liuyan) {
            //我收到的产品留言（企业账号）
            mHolder.collection_title.setText(new StringBuffer(list.get(position).getNickname()).append("   在").append("<<").
                    append(list.get(position).getTitle()).append(">>  ").append("  给我留言"));
        }
        mHolder.collection_time.setText(list.get(position).getCreated_at().substring(0, 10));
        final int a = position;

        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (come_from == Contant.come_from_my_skill_zixun) {
                    //跳转到技术详情页面
                    Intent intent = new Intent(mContext, Main_Skill_DetailsActivity.class);
                    intent.putExtra("skill_id", list.get(a).getId() + "");

                    mContext.startActivity(intent);
                } else if (come_from == Contant.come_from_my_product_liuyan) {
                    //跳转到留言详情页
                    Intent intent = new Intent(mContext, Main_Liuyan_DetailActivity.class);
                    intent.putExtra("liuyan_qiye_name", list.get(a).getNickname());
                    intent.putExtra("liuyan_qiye_content", list.get(a).getDetail());
                    intent.putExtra("liuyan_qiye_time", list.get(a).getCreated_at());
                    mContext.startActivity(intent);
                } else if (come_from == Contant.come_from_my_service_liuyan) {
                    //跳转到服务留言详情
                    Intent intent = new Intent(mContext, Main_Liuyan_DetailActivity.class);
                    intent.putExtra("liuyan_qiye_name", list.get(a).getNickname());
                    intent.putExtra("liuyan_qiye_content", list.get(a).getDetail());
                    intent.putExtra("liuyan_qiye_time", list.get(a).getCreated_at());
                    mContext.startActivity(intent);
                } else if (come_from == Contant.come_from_my_qiye_liuyan) {
                    //跳转到企业留言详情
                    Intent intent = new Intent(mContext, Main_Liuyan_DetailActivity.class);
                    intent.putExtra("liuyan_qiye_name", list.get(a).getNickname());
                    intent.putExtra("liuyan_qiye_content", list.get(a).getDetail());
                    intent.putExtra("liuyan_qiye_time", list.get(a).getCreated_at());
                    mContext.startActivity(intent);
                } else if (come_from == Contant.come_from_my_receiver_qiye_liuyan) {
                    //跳转到企业留言详情
                    Intent intent = new Intent(mContext, Main_Liuyan_DetailActivity.class);
                    intent.putExtra("liuyan_qiye_name", list.get(a).getNickname());
                    intent.putExtra("liuyan_qiye_content", list.get(a).getDetail());
                    intent.putExtra("liuyan_qiye_time", list.get(a).getCreated_at());
                    mContext.startActivity(intent);
                } else if (come_from == Contant.come_from_my_receiver_fuwu_liuyan) {
                    //跳转到企业留言详情
                    Intent intent = new Intent(mContext, Main_Liuyan_DetailActivity.class);
                    intent.putExtra("liuyan_qiye_name", list.get(a).getNickname());
                    intent.putExtra("liuyan_qiye_content", list.get(a).getDetail());
                    intent.putExtra("liuyan_qiye_time", list.get(a).getCreated_at());
                    mContext.startActivity(intent);
                } else if (come_from == Contant.come_from_my_receiver_product_liuyan) {
                    //跳转到企业留言详情
                    Intent intent = new Intent(mContext, Main_Liuyan_DetailActivity.class);
                    intent.putExtra("liuyan_qiye_name", list.get(a).getNickname());
                    intent.putExtra("liuyan_qiye_content", list.get(a).getDetail());
                    intent.putExtra("liuyan_qiye_time", list.get(a).getCreated_at());
                    mContext.startActivity(intent);
                }
            }
        });
        return convertView;
    }

    class Holder {
        TextView collection_title, zixun_content;
        TextView collection_time;
        Button button_delect;
    }
}
